import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import java.util.Vector;


public class Quiz
{
	public Quiz()
	{
		
	}

	public Quiz(String quizname)
	{
		this.setQuizname(quizname);
	}
	
	/*
	 * 
	 * Code for parsing XML into java objects from - http://www.java-samples.com/showtutorial.php?tutorialid=152
	 */
	public void load(String filename)
	{
		//parse the xml file and get the dom object
		parseXmlFile(filename);
		//get each Question and create a corresponding object
		parseDocument();
	}
	
	
	private void parseXmlFile(String filename)
	{
		//get the factory
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		
		try
		{
			// Using factory get an instance of document builder
			DocumentBuilder db = dbf.newDocumentBuilder();
			// parse using builder to get DOM representation of the XML file
			dom = db.parse(filename);
		}
		catch(ParserConfigurationException pce)
		{
			pce.printStackTrace();
		}
		catch(SAXException se)
		{
			se.printStackTrace();
		}catch(IOException ioe)
		{
			ioe.printStackTrace();
		}
	}
	
	
	private void parseDocument()
	{
		//get the root elememt
		Element docEle = dom.getDocumentElement();
		
		// get the quiz name
		NodeList nl = docEle.getElementsByTagName("Name");
		Element nameEl = (Element)nl.item(0);
		if(nameEl != null)
		{
			String name = nameEl.getTextContent();
			setQuizname(name);
		}
		
		//get a nodelist of <question> elements
		 nl = docEle.getElementsByTagName("Question");
		if(nl != null && nl.getLength() > 0) 
		{
			for(int i = 0 ; i < nl.getLength();i++) 
			{
				// get the question element
				Element el = (Element)nl.item(i);
				// get the Question object
				Question q = createQuestion(el);
				// add it to vector
				addQuestion(q);
			}
		}
	}
	
	
	private Question createQuestion(Element el)
	{	
		String title = getTextValue(el,"Title");
		String text = getTextValue(el, "Text");
		String answer = getTextValue(el, "Answer");
		
		String elType = el.getAttribute("type");
		if(elType.equals("Multiple"))
		{
			String imageFile = getTextValue(el, "File");
			MultipleChoiceQuestion mcq = new MultipleChoiceQuestion(title, text, answer, imageFile);
			for(int i = 0; i < 4; ++i)
			{
				String selection = getTextValue(el, "S" + Integer.toString(i + 1));
				mcq.setSelection(i, selection);
			}
			return mcq;
		}
		else if(elType.equals("Time"))
		{
			return new TimeQuestion(title, text, answer);
		}
		else
		{
			return null;
		}
	}
	
	/**
	 * I take a xml element and the tag name, look for the tag and get
	 * the text content 
	 * i.e for <employee><name>John</name></employee> xml snippet if
	 * the Element points to employee node and tagName is name I will return John  
	 * @param ele
	 * @param tagName
	 * @return
	 */
	private String getTextValue(Element ele, String tagName)
	{
		String textVal = null;
		NodeList nl = ele.getElementsByTagName(tagName);
		if(nl != null && nl.getLength() > 0)
		{
			Element el = (Element)nl.item(0);
			textVal = el.getFirstChild().getNodeValue();
		}
		return textVal;
	}
	
	public int size()
	{
		return itsQuestion.size();
	}
	
	public Question getQuestion(int index)
	{
		if(index > -1 && index < itsQuestion.size())
		{
			return itsQuestion.get(index);
		}
		else
		{
			return null;
		}
	}
	
	public void addQuestion(int index, Question q)
	{
		itsQuestion.add(index, q);
	}
	
	public void addQuestion(Question q)
	{
		itsQuestion.add(q);
	}
	
	public void removeQuestion(int index)
	{
		if(index > -1 && index < itsQuestion.size())
		{
			itsQuestion.remove(index);
		}
	}

	public String getQuizname()
	{
		return quizname;
	}

	public void setQuizname(String quizname)
	{
		this.quizname = quizname;
	}


	private Vector<Question> itsQuestion = new Vector<Question>();
	private String quizname;
	Document dom;
}
